package com.study.controller;

import com.study.Util.BookTypeUtil;
import com.study.factory.HttpClient;
import com.study.pojo.Book;
import com.study.pojo.Orders;
import com.study.pojo.Read;
import com.study.pojo.User;
import com.study.service.BookService;
import com.study.service.OrdersService;
import com.study.service.ReadService;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 首页控制器
 * @Author Miss kun
 * @Date 2020/10/10 16:07
 * @Version 1.0
 */
@Controller
public class TotalController {

    @Autowired
    private ReadService readService;

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 项目启动之后，跳转到首页
     * @return
     */
    @RequestMapping("/")
    public String index(HttpSession session,Model model){
        //获取随机20本图书显示到前端页面
        List<Book> books = bookService.getBooks();
        //获取根据类别分类的排行榜
        List<Book> rankBooks = bookService.getBookByRankList();
        model.addAttribute("books",books);
        model.addAttribute("rankBooks",rankBooks);
        return "index";
    }

    /**
     * 跳转到登陆注册
     * @return
     */
    @RequestMapping("/index/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/index/toIndex")
    public String toIndex(){
        //重定向到首页
        return "redirect:/";
    }

    /**
     * 跳转到每日一读页面
     */
    @RequestMapping("/index/toRead")
    public String toRead(Model model){
        System.out.println("toRead------>");
        //获取随机每日一读的内容
        Read content = readService.getContent();
        //将数据存到model中，然后在read.html中通过thymeleaf显示
        model.addAttribute("read",content);
        return "read";
    }

    @RequestMapping("/index/toHtml")
    @ResponseBody
    public void  toHtml(String Url, HttpServletResponse response){
        response.setContentType("image/jpeg;charset=UTF-8");
        String fileName = "a.jpg";
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        HttpClient.getURL(Url,response);
        return;
    }

    @RequestMapping("/index/toBookInfo/{bookId}")
    public String toBookInfo(@PathVariable("bookId") String bookId,Model model){
        System.out.println("toBookInfo---->"+bookId);
        List<Book> chapterBooks = bookService.getChaptersByBookId(Integer.parseInt(bookId));
        List<Book> rankBooks = bookService.getBookRankByType(chapterBooks.get(0).getType());
        model.addAttribute("rankBooks",rankBooks);
        model.addAttribute("chapterBooks", chapterBooks.get(0));
        return "bookInfo";
    }

    /**
     * 跳转到小说页面,展示30条数据到前端
     * @return
     */
    @RequestMapping("/index/toNovel/{bookType}")
    public String toNovel(@PathVariable int bookType, Model model){
        System.out.println("toNovel----->"+bookType);
        List<Book> novelBooks = null;
        List<Book> rankBooks = null;
        Integer counts = null;
        switch (bookType){
            case 1:
                //1：小说作品
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.novelType);
                counts = bookService.getCountByType(BookTypeUtil.novelType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.novelType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "novel";
            case 2:
                //2：经济管理
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.economyType);
                counts = bookService.getCountByType(BookTypeUtil.economyType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.economyType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "economy";
            case 3:
                //3：历史传记
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.historyType);
                counts = bookService.getCountByType(BookTypeUtil.historyType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.historyType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "history";
            case 4:
                //4：生活艺术
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.lifeType);
                counts = bookService.getCountByType(BookTypeUtil.lifeType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.lifeType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "life";
            case 5:
                //5：文学艺术
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.literatureType);
                counts = bookService.getCountByType(BookTypeUtil.literatureType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.literatureType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "literature";
            case 6:
                //6：社会科学
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.societyType);
                counts = bookService.getCountByType(BookTypeUtil.societyType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.societyType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "society";
            case 7:
                // 7：成功励志
                novelBooks = bookService.getBookByPage(1,30, BookTypeUtil.successType);
                counts = bookService.getCountByType(BookTypeUtil.successType);
                rankBooks = bookService.getBookRankByType(BookTypeUtil.successType);
                model.addAttribute("rankBooks",rankBooks);
                model.addAttribute("counts",counts);
                model.addAttribute("novelBooks",novelBooks);
                return "success";
            default:
        }
        return "redirect:/";
    }

    /**
     *展示分页数据
     * @return
     */
    @RequestMapping("/index/doNovel/{bookType}")
    @ResponseBody
    public List<Book> doNovel(@PathVariable int bookType,@RequestParam("page") int page , Model model){
        System.out.println("doNovel------>bookType:"+bookType+"page:"+page);
        List<Book> novelBooks = null;
        switch (bookType) {
            case 1:
                //1：小说作品
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.novelType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 2:
                //2：经济管理
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.economyType);
                model.addAttribute("novelBooks", novelBooks);
                break;

            case 3:
                //3：历史传记
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.historyType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 4:
                //4：生活艺术
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.lifeType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 5:
                //5：文学艺术
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.literatureType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 6:
                //6：社会科学
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.societyType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 7:
                // 7：成功励志
                novelBooks = bookService.getBookByPage(page, 30, BookTypeUtil.successType);
                model.addAttribute("novelBooks", novelBooks);
                break;
            default:
        }
        return novelBooks;
    }


    /**
     * 只显示8条到首页
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("index/toSearch")
    public String toSearch(@RequestParam("name") String name, Model model){
        System.out.println("toSearch------>"+name);
        List<Book> books = bookService.getBookByLike(name,1,8);
        Integer count = bookService.getCountByLikes(name);
        List<Book> bookByRankList = bookService.getBookByRankList();
        model.addAttribute("books",books);
        model.addAttribute("counts",count);
        model.addAttribute("name",name);
        model.addAttribute("rankBooks",bookByRankList);
        return "search";
    }

    @RequestMapping("index/doSearch")
    @ResponseBody
    public List<Book> doSearch(@RequestParam("name") String name, @RequestParam("page") int page, Model model){
        System.out.println("doSearch------>name:"+name+"page:"+page);
        List<Book> books = bookService.getBookByLike(name,page,8);
        Integer count = bookService.getCountByLikes(name);
        model.addAttribute("books",books);
        model.addAttribute("counts",count);
        return books;
    }

    @RequestMapping("/index/toAccount/{phone}")
    public String toAccount(@PathVariable String phone,Model model){
        System.out.println("toAccount"+phone);
        User user = userService.findByPhone(phone);
        model.addAttribute("user",user);
        return "accountInfo";

    }

    /**
     * 跟新账户信息
     * @param user
     * @return
     */
    @RequestMapping("/index/doUpdateAccount")
    @ResponseBody
    public boolean doUpdateAccountInfo(User user){
        System.out.println("doUpdateAccount"+user);
        userService.updateUser(user);
        return true;
    }

    @RequestMapping("/index/toBookshelf/{userId}")
    public String  toMyBookshelf(@PathVariable Integer userId ,Model model){
        System.out.println("toBookshelf------>"+userId);
        if (userId == 0){
            return "bookshelf";
        }

        //查找当前用户订阅的图书，同时包含章节数等信息
        List<Orders> books = ordersService.getMyOrdersBook(userId, 1);
        System.out.println();
        List<Book> bookByRankList = bookService.getBookByRankList();
        model.addAttribute("books",books);
        model.addAttribute("rankBooks",bookByRankList);
        model.addAttribute("counts",books.size());
        return "bookshelf";
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/index/nev")
    public String toNev(HttpSession session){
        return "nev";
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/index/exit")
    public String exitSession(HttpSession session){
       session.removeAttribute("user");
        return "redirect:/";
    }

}


